﻿using System;

namespace IceCS.DateEdge.Core
{
    /// <summary>
    /// 时间边缘帮助类
    /// </summary>
    public static class DateEdgeHelper
    {
        /// <summary>
        /// 获取时间最大值
        /// </summary>
        public static DateTime GetMaxValue()
        {
            return DateTime.MaxValue;
        }

        /// <summary>
        /// 获取时间最小值
        /// </summary>
        public static DateTime GetMinValue()
        {
            return DateTime.MinValue;
        }

        /// <summary>
        /// 获取当前时间
        /// </summary>
        public static DateTime GetNow()
        {
            return DateTime.Now;
        }

        /// <summary>
        /// 获取今天开始时间
        /// </summary>
        public static DateTime GetTodayBegin()
        {
            var now = DateTime.Now;
            return new DateTime(now.Year, now.Month, now.Day, 0, 0, 0);
        }

        /// <summary>
        /// 获取今天结束时间
        /// </summary>
        public static DateTime GetTodayEnd()
        {
            var now = DateTime.Now;
            now = now.AddDays(1);
            return new DateTime(now.Year, now.Month, now.Day, 0, 0, 0);
        }

        /// <summary>
        /// 获取昨天开始时间
        /// </summary>
        public static DateTime GetYesterdayBegin()
        {
            var now = DateTime.Now;
            now = now.AddDays(-1);
            return new DateTime(now.Year, now.Month, now.Day, 0, 0, 0);
        }

        /// <summary>
        /// 获取昨天结束时间
        /// </summary>
        public static DateTime GetYesterdayEnd()
        {
            return GetTodayBegin();
        }

        /// <summary>
        /// 获取明天开始时间
        /// </summary>
        public static DateTime GetTomorrowBegin()
        {
            return GetTodayEnd();
        }

        /// <summary>
        /// 获取明天结束时间
        /// </summary>
        public static DateTime GetTomorrowEnd()
        {
            var now = GetTomorrowBegin();
            now = now.AddDays(1);
            return new DateTime(now.Year, now.Month, now.Day, 0, 0, 0);
        }

        /// <summary>
        /// 获取本周开始时间
        /// </summary>
        /// <param name="isStartByMonday ">是否以本周星期一的 00:00:00 刻为开始日期，默认true,如果是false，则以本周星期日的 00:00:00 刻为开始日期</param>
        /// <returns></returns>
        public static DateTime GetThisWeekBegin(bool isStartByMonday = true)
        {
            var now = DateTime.Now;
            //这里的day表示天，星期用 dayOfWeek 表示
            var day = now.Day;
            var dayOfWeek = (int)now.DayOfWeek;//星期日的值是0
            var dayValue = 0;
            if (isStartByMonday)
            {
                //如果以星期一为开始，那么星期天应该是7
                if (dayOfWeek == 0)
                {
                    dayOfWeek = 7;
                }
                dayValue = day - dayOfWeek + 1;
            }
            else
            {
                dayValue = day - dayOfWeek;
            }
            //本周起始时间和今天日期间隔的天数
            var weekTimeSpan = dayValue - now.Day;
            now = now.AddDays(weekTimeSpan);
            return new DateTime(now.Year, now.Month, now.Day, 0, 0, 0);
        }

        /// <summary>
        /// 获取本周结束时间
        /// </summary>
        /// <param name="isStartByMonday">是否以本周星期一的 00:00:00 刻为开始日期，默认true,如果是false，则以本周星期日的 00:00:00 刻为开始日期</param>
        /// <returns></returns>
        public static DateTime GetThisWeekEnd(bool isStartByMonday = true)
        {
            var now = GetThisWeekBegin(isStartByMonday);
            now = now.AddDays(7);
            return new DateTime(now.Year, now.Month, now.Day, 0, 0, 0);
        }
    }
}
